package Principal;





import java.io.*; 

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.antlr.runtime.CharStream;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenRewriteStream;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.antlr.runtime.tree.DOTTreeGenerator;
import org.antlr.runtime.tree.TreeAdaptor;
import org.antlr.stringtemplate.StringTemplate;

import antlr.ANTLRException;




public class Principal { 
	
	
	public Principal(String arg) throws ANTLRException {
	
			LeerFichero(arg,"");
	}

	public Principal(String arg,String arg2) throws ANTLRException {
		
		LeerFichero(arg,arg2);
	}
	public Principal() {
		// TODO Auto-generated constructor stub
	}

	
	private static void LeerFichero(String arg,String arg2) {
		try { 
			CharStream fis = null;
			fis = new ANTLRFileStream(arg);
			//FileInputStream fis = new FileInputStream(arg); 
			Analex analex = null;	 
			Anasin anasint = null; 
			Evaluador evaluador = null;
			
			analex = new Analex (fis); 
			anasint = new Anasin (new CommonTokenStream(analex));
			anasint.entrada();	
			
			System.out.println("Cargando fichero de codigo SAM");
			LeerFichero2("test1_out.txt");
			
			
		}catch(RecognitionException ae) { 
			System.err.println(ae.getMessage()); 
		} 
		catch(IOException e ) { 
			System.err.println("No se encontro el fichero"); 
		}
	}


	private static void LeerFichero2(String arg) {
		try { 
    	    CharStream fis = null;
			fis = new ANTLRFileStream(arg);
			AnalexSAM analex = null;	 
			AnasinSAM anasint = null; 
			Evaluador evaluador = null;
			
			CommonTree arbol = null;
			analex = new AnalexSAM (fis); 
			CommonTokenStream tokens = new CommonTokenStream (analex);
			anasint = new AnasinSAM (tokens);
			anasint.instrucciones ();

		}catch(RecognitionException ae) { 
			System.err.println(ae.getMessage()); 
		} 
		catch(IOException e ) { 
			System.err.println("No se encontro el fichero"); 
		}
	}

	
	
	public static void main(String args[]) { 
	
		System.out.println(args[0]);		
		switch(args[0].charAt(4)) {		
			case '1': LeerFichero(args[0],args[1]);break;
			case '2': LeerFichero2(args[0]);break;
			case '3': LeerFichero(args[0],args[1]);break;
	//		case '4': LeerFichero(args[0]);break;
	//		case '5': LeerFichero5(args[0]);break;
		}
		
		//LeerFichero(args[0]);
		
	} 
	
	
}
	